<?php
/*
* 推广会员投标
*/
class PtenderBehavior extends Behavior
{
    protected $options   =  array('TENDER_ON'=>true);

    public function run(&$params=array())
	{
		if($this->options['TENDER_ON'])
		{
			$DB = C('JD_DB');
			$exists = M()->table($DB.'popularize_member AS pm')->join($DB.'popularize_user AS pu ON pu.userid=pm.userid')
				->where("pm.memberid IS NOT NULL AND pm.memberid={$params['memberid']} AND pu.userid IS NOT NULL AND pu.percent > 0")
				->field('pm.memberid,pu.userid,pu.percent')->find();
			if(empty($exists) || empty($exists['userid']) || empty($exists['percent'])) return;
			$percentmoney = sprintf("%1\$.3f",$exists['percent'] * $params['money'] / 100);
			$SQL = "INSERT INTO {$DB}popularize_member_tender (`memberid`,`loansn`,`tendersn`,`tendermoney`,`percent`,`percentmoney`,`timeadd`,`status`) VALUES ";
			$SQL.= "('{$params['memberid']}','{$params['loansn']}','{$params['tendersn']}','{$params['money']}','{$exists['percent']}','{$percentmoney}',NOW(),0)";
			$F = M()->query($SQL);
			$G = M($DB.'popularize_member')->where(array('memberid'=>$params['memberid']))->setInc('tendermoney',$params['money']);
			return ($F===false || $G===false) ? false : true;
		}
    }
}

?>